package com.m1905hd.user.service.impl

import com.m1905hd.common.util.Page
import com.m1905hd.user.service.BaseService
import org.mybatis.spring.support.SqlSessionDaoSupport

/**
 *
 * @author weiwei.huang
 * @email ihuangweiwei@163.com
 * @date 15/4/2
 */
abstract class BaseServiceImpl<T> extends SqlSessionDaoSupport implements BaseService<T> {


    @Override
    T create(T t) {
        getSqlSession().insert(getNameSpace() + "insert", t) == 1 ? t : null
    }

    @Override
    T update(T t) {
        getSqlSession().update(getNameSpace() + "update", t) == 1 ? t : null
    }

    @Override
    boolean delete(int id) {
        return getSqlSession().delete(getNameSpace() + "delete", id)
    }

    @Override
    T find(int id) {
        return getSqlSession().selectOne(getNameSpace() + "findOne", id)
    }

    @Override
    Page list(Map<String, Object> query, int page, int size) {
        int count = getSqlSession().selectOne(getNameSpace() + "findCount", query)
        if (size < 0 || size >= 20) {
            size = 20;
        }
        if (page <= 0) {
            page = 1;
        }
        Page p = new Page(count, page, size);
        query.put("start", (page - 1) * size)
        query.put("offset", size)
        List list = getSqlSession().selectList(getNameSpace() + "getPage", query)
        list.each {

        }
        p.setData(list)
        return p
    }

    abstract String getNameSpace();
}

